package com.coldchain.admin.template.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.coldchain.admin.template.entity.ExpressionTemp;
import com.coldchain.admin.template.entity.FreightTempConfig;
import com.coldchain.admin.template.service.IExpressionService;
import com.coldchain.admin.template.service.IFreightTempConfigService;
import com.coldchain.authclient.annotation.CheckClientToken;
import com.coldchain.authclient.annotation.CheckUserToken;
import com.coldchain.common.msg.ObjectRestResponse;
import com.fangliai.core.context.BaseContextHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统计费模板 前端控制器
 * </p>
 *
 * @author fangliai
 * @since 2018-09-13
 */
@RestController
@RequestMapping("/template")
@CheckClientToken
@CheckUserToken
public class ExpressionTempController {

    @Autowired
    private IExpressionService billingTempService;

    @Autowired
    private IFreightTempConfigService freightTempConfigService;


    @RequestMapping("/list")
    public ObjectRestResponse<ExpressionTemp> list(){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByDesc("createTime");
        return new ObjectRestResponse<>().data(billingTempService.list(wrapper));
    }

    /**
     * 修改收费模板
     * @param billingTemp
     * @return
     */
    @PostMapping("/edit")
    public Object edit(ExpressionTemp billingTemp){
        if(StringUtils.isEmpty(billingTemp.getTempId())){
            billingTemp.setCreateBy(BaseContextHandler.getUsername());
            billingTemp.setCreateTime(new Date().getTime());
        }
        billingTempService.saveOrUpdate(billingTemp);
        return new ObjectRestResponse<>().succeed();
    }

    /**
     * 删除收费板
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public Object del(String id){
        billingTempService.removeById(id);
        return new ObjectRestResponse<>().succeed();
    }

    /**
     * 设置收费模板
     * @param configs
     * @return
     */
    @PostMapping("/setting")
    public Object settigFreightTemplate(List<FreightTempConfig> configs){
            freightTempConfigService.saveBatch(configs);
            return  new ObjectRestResponse<>().succeed();
    }


}
